<template>
  <div class="chart-detail">
    <div class="" style="height:70px;" v-show="canShow"></div>
    <!-- 头部显示 -->
    <div class="detail-top">
      <div class="top-left">
        养老支出月度分析
      </div>
      <div class="top-right" @click="goBack">
        返回
      </div>
    </div>
    <!-- 详情 -->
    <div class="detail-box" :style="{height:height+'px'}">
      <!-- 详情头部 -->
      <h3 class="chart-title">
        <div class="area-title-name" @mouseenter="mouseenter()"
             @mouseleave="mouseleave()">{{ areaTitle }} &nbsp; | &nbsp; {{title}}年
        </div>
      </h3>
      <div class="chart-title-floating" v-show="showFloating">
        <span>统筹区: {{ this.propParams.areaName }}</span><br>
        <span>年份: {{ title }}</span>
      </div>
      <div id="pension-expenditure-analysis-year-detail" class="detail-content"></div>
    </div>
  </div>
</template>
<script>

  import '../../assets/css/chartDetail.css'
  import {loadingOpt} from '../../assets/js/utils.js'
  import {getMonthPensionExpenditure} from '../../api/api'


  export default {
    name: 'chartDetail',
    props: ['isChange'],
    data () {
      return {
        title: '',
        areaTitle: '',
        isShowChart: true,
        height: 800,
        canShow: true,
        showFloating: false,
        propParams: {}
      }
    },
    mounted () {
      this.propParams = this.$route.query;
      this.title = this.propParams.year;
      this.areaTitle = this.filterLogText(this.propParams.areaName);

      this.getContentH();
      setTimeout(() => {
        this.myChart = this.$echarts.init(document.getElementById('pension-expenditure-analysis-year-detail'));
        this.myChart.showLoading(loadingOpt);
        this.getData();
      }, 101)
    },
    watch: {
      isChange () {
        this.getContentH();
      },
    },
    methods: {
      getData () {
        getMonthPensionExpenditure('pensionExpenditure/monthPensionExpenditure', this.propParams.year, this.propParams.areaCode,)
          .then(response => {
            let json = response.data;
            if (response.code === 0) {
              this.myChart.hideLoading();

              this.conclusion = json.reportConclusion;

              this.drawLine(json)
            }
          })
      },

      // 设置高度
      getContentH () {
        setTimeout(() => {
          let height = document.documentElement.clientHeight;
          let barHeight = 0;
          if (document.querySelector('.el-header')) {
            this.canShow = true;
            barHeight = document.querySelector('.el-header').clientHeight;
          } else {
            this.canShow = false;
          }
          this.height = height - barHeight - 96;
        }, 100);
        if (this.myChart) {
          setTimeout(() => {
            this.myChart.resize();
          }, 301)
        }
      },
      goBack () {
        this.$router.push(this.propParams.hash.slice(1,this.propParams.hash.length));
        // this.$router.go(-1);
      },
      // 字数太长省略
      filterLogText (value, leng = 7) {
        if (value.length <= leng) {
          return value;
        } else {
          return value.slice(0, leng) + '...';
        }
      },
      drawLine (json) {
        // 格式化负增长数据，为0时设为空
        json.negativeIncreaseQuotaList.map((item, index) => {
          if (item == 0) {
            json.negativeIncreaseQuotaList[index] = '';
          }
        });

        let barFormatter = function (params) {
          let str = json.positiveIncreaseQuotaList[params.dataIndex] > json.negativeIncreaseQuotaList[params.dataIndex] ? '增' : '减';
          return `<span style="color: #fff">养老支出</span></br>
                          <span style="color: #fff">月份: ${params.name}</span></br>
                          <span style="color: #fff">
                          支出金额: ${Number((json.yearlyTotalCostList[params.dataIndex]
            + json.positiveIncreaseQuotaList[params.dataIndex]).toFixed(2))}万元
                          </span></br>
                          <span>比上月${str}: </span>
                          <span style="color: #fff">${
            (json.positiveIncreaseQuotaList[params.dataIndex]
              > json.negativeIncreaseQuotaList[params.dataIndex])
              ? json.positiveIncreaseQuotaList[params.dataIndex]
              : json.negativeIncreaseQuotaList[params.dataIndex]}万元</span>`
        }

        let option = {
          grid: [//此参数可在一个dom中划分多个图表 可设置不同图表的位置
            {
              top: '10%',
              height: '12%',
              tooltip: {
                formatter: "{b}</br>{c}%" //给当前坐标系添加弹窗
              }
            },
            {
              top: '36%',
              height: '46%',
              tooltip: {
                show: false,
              }
            }
          ],
          legend: [
            {
              left: 'center',
              icon: 'circle',
              itemWidth: 10,
              itemHeight: 10,
              itemGap: 20,
              selectedMode: false,
              tooltip: {
                show: true,
                formatter: function (params) {
                  return '增长率';
                }
              },
              data: ['增长率'],
              textStyle: {
                color: '#fff'
              },
            },
            {
              top: '92%',
              left: 'center',
              icon: 'rect',
              itemWidth: 10,
              itemHeight: 10,
              itemGap: 20,
              selectedMode: false,
              tooltip: {
                show: true,
                formatter: function (params) {
                  let txt = '';
                  if (params.name == '增长额(正)') {
                    txt = '增长额(正)'
                  } else if (params.name == '增长额(负)') {
                    txt = '增长额(负)'
                  } else if (params.name == '养老支出金额') {
                    txt = '养老支出金额'
                  }
                  return txt;
                }
              },
              data: ['养老支出金额',
                '增长额(正)',
                '增长额(负)',
              ],
              textStyle: {
                color: '#fff'
              },
            }],
          tooltip: {
            show: true,
            textStyle: {
              fontSize: 12
            }
          },
          xAxis: [
            {
              data: json.monthList,
              show: true,
              axisLine: {
                show: true, // 显示x轴坐标线并设置为虚线
                lineStyle: {
                  color: '#294363'
                }
              },
              axisTick: {
                show: false // 不显示x轴坐标的刻度
              },
              axisLabel: {
                show: false // 不显示x轴坐标数值
              }
            },
            {
              data: json.monthList,
              gridIndex: 1,
              axisLine: { // x轴的横线是否显示
                show: true,
                lineStyle: {
                  color: '#2a4565'
                }
              },
              axisTick: { // x轴的刻度短竖线是否显示
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            }
          ],
          yAxis: [
            {
              splitLine: {
                show: false,
              },
              position: 'right',
              splitNumber: 2,
              axisLine: {
                show: false,
              },
              axisTick: {
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            },
            {
              name: '单位 : 万元',
              nameGap: '25',
              nameTextStyle: {
                color: '#fff',
              },
              splitLine: {
                show: true,
                lineStyle: {
                  color: '#2a4565',
                  type: 'dashed',
                }
              },
              gridIndex: 1,
              axisLine: {
                show: true,
                lineStyle: {
                  color: '#2a4565',
                }
              },
              axisTick: {
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            }
          ],
          series: [ // 折线
            {
              type: 'line',
              color: '#129cff',
              symbol: 'circle',
              showSymbol: true,
              name: '增长率',
              data: json.yearlyGrowthRateList,
              symbolSize: 10,
              tooltip: {
                show: true,
                formatter: function (params) {
                  return `<sapn style="color: #fff">增长率</sapn></br>
                          <span style="color: #fff">月份: ${params.name}</span></br>
                          <span style='color:#fff'>增长率: ${params.value}%</span>`
                }
              },
              label: {
                show: true,
                position: 'top',
                formatter: '{c}%',
                color: '#fff'
              }
            },
            {
              type: 'bar',
              name: '养老支出金额',
              stack: '总额',
              data: json.yearlyTotalCostList,
              color: '#ff6a93',
              barWidth: '30%',
              xAxisIndex: 1,
              yAxisIndex: 1,
              z: 2,
              tooltip: {
                show: true,
                formatter: barFormatter
              },
            },
            {
              type: 'bar',
              name: '增长额(正)',//此值与legend图例对应
              stack: '总额',
              color: '#ffd66a',
              data: json.positiveIncreaseQuotaList,

              label: {
                show: true,
                color: '#fff',
                position: 'top',
                formatter: function (params) {
                  let val = '';
                  let arr = json.yearlyTotalCostList;
                  arr.map((item, index) => {
                    if (params.dataIndex == index) {
                      if (Math.round((arr[index] + params.value) * 100) % 10 == 0) {
                        val = (arr[index] + params.value).toFixed(1)
                      } else if (Math.round((arr[index] + params.value) * 100) % 100 == 0) {
                        val = (arr[index] + params.value).toFixed(0)
                      } else {
                        val = (arr[index] + params.value).toFixed(2)
                      }
                    }
                  });
                  return Number(val);
                }
              },
              tooltip: {
                show: true,
                formatter: barFormatter
              },
              barWidth: '30%',
              xAxisIndex: 1,
              yAxisIndex: 1,
            },
            {
              type: 'bar',
              name: '增长额(负)',
              stack: '总额',
              showSymbol: false,
              data: json.negativeIncreaseQuotaList,
              z: 1,
              itemStyle: {
                color: '#263b5e',
              },
              tooltip: {
                show: true,
                formatter: barFormatter
              },
              barWidth: '30%',
              xAxisIndex: 1,
              yAxisIndex: 1
            }
          ],
        };

        this.myChart.setOption(option);
      },
      mouseenter () {
        this.showFloating = true;
      },
      mouseleave () {
        this.showFloating = false;
      },
    }
  }
</script>
<style scoped>
</style>
